﻿Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports OMANE.Modules.TauxGarantis.ViewModels
Imports OMANE.Common.UI

Namespace Views
    Partial Public Class ViewCalculDetaillesModel2
        Public Sub New()
            MyBase.New()

            Me.InitializeComponent()

            ' Insert code required on object creation below this point.
        End Sub


        Private Sub SimpleGrd_Grouper_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            If simpleDataGrid.SelectedItems Is Nothing Then Return
            If simpleDataGrid.SelectedItems.Count < 1 Then Return
            Dim vm As ViewModelCalculsDetaillesModel2 = TryCast(DataContext, ViewModelCalculsDetaillesModel2)
            If vm Is Nothing Then Return

            Dim originalrows As New List(Of Integer)
            Return
            For Each rv As DataRowView In simpleDataGrid.SelectedItems
                For Each r As DataRow In rv.Row.GetChildRows("GroupToLink")
                    If Not originalrows.Contains(CInt(r("SourceID"))) Then
                        originalrows.Add(CInt(r("SourceID")))
                    End If
                Next
            Next
            For Each rv As DataRowView In simpleDataGrid.SelectedItems
                For Each r As DataRow In rv.Row.GetChildRows("GroupToLink")
                    vm.LinkTable.Rows.Remove(r)
                Next
            Next
            Dim grows As New List(Of DataRow)
            For Each r As DataRowView In simpleDataGrid.SelectedItems
                grows.Add(r.Row)
            Next
            For Each r As DataRow In grows
                vm.GroupTable.Rows.Remove(r)
            Next
            grows.Clear()
            grows = Nothing
            Dim rg = vm.GroupTable.NewRow()

            vm.GroupTable.Rows.Add(rg)

            For Each elt As Integer In originalrows
                vm.LinkTable.Rows.Add(rg("GroupID"), elt, CDbl(1))
            Next

            ReCalculate(rg)
        End Sub
        Private Sub SimpleGrd_DeGrouper_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
            If simpleDataGrid.SelectedItems Is Nothing Then Return
            If simpleDataGrid.SelectedItems.Count < 1 Then Return
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)
            If vm Is Nothing Then Return

            Dim originalrows As New List(Of Integer)
            For Each rv As DataRowView In simpleDataGrid.SelectedItems
                For Each r As DataRow In rv.Row.GetChildRows("GroupToLink")
                    If Not originalrows.Contains(CInt(r("SourceID"))) Then
                        originalrows.Add(CInt(r("SourceID")))
                    End If
                Next
            Next

            For Each rv As DataRowView In simpleDataGrid.SelectedItems
                For Each r As DataRow In rv.Row.GetChildRows("GroupToLink")
                    vm.LinkTable.Rows.Remove(r)
                Next
            Next
            Dim grows As New List(Of DataRow)
            For Each r As DataRowView In simpleDataGrid.SelectedItems
                grows.Add(r.Row)
            Next
            For Each r As DataRow In grows
                vm.GroupTable.Rows.Remove(r)
            Next
            grows.Clear()
            grows = Nothing


            Dim newrows As New List(Of DataRow)
            For Each elt As Integer In originalrows
                Dim rg = vm.GroupTable.NewRow()
                vm.GroupTable.Rows.Add(rg)
                newrows.Add(rg)
                vm.LinkTable.Rows.Add(rg("GroupID"), elt, CDbl(1))
            Next
            For Each r In newrows
                ReCalculate(r)
            Next
            newrows.Clear()
            newrows = Nothing
        End Sub
        Private Sub ReCalculate(ByVal groupRow As DataRow)
            Dim vm As ViewModelCalculsDetailles = TryCast(DataContext, ViewModelCalculsDetailles)

            Dim weightcol As String = If(vm.OriginalTable.Columns.Contains("CSR"), "CSR", "Weight")

            Dim qtot As Double = Aggregate r In groupRow.GetChildRows("GroupToLink")
                                 Into Sum(CDbl(r.GetParentRow("SourceToLink")(weightcol)))

            For Each col As DataColumn In vm.GroupTable.Columns
                If col.ColumnName = "GroupID" Then
                ElseIf col.ColumnName = "SumOfWeight" Then
                    groupRow(col) = groupRow.GetChildRows("GroupToLink").Length
                ElseIf col.ColumnName = weightcol Then
                    groupRow(col) = qtot
                ElseIf col.ColumnName = "POIDS_CSR" Then

                Else
                    If vm.OriginalTable.Columns.Contains(col.ColumnName) Then
                        If col.DataType.IsValueType Then
                            Dim qagg = Aggregate r In groupRow.GetChildRows("GroupToLink")
                                       Into Sum(CDbl(r.GetParentRow("SourceToLink")(weightcol)) * CDbl(r.GetParentRow("SourceToLink")(col.ColumnName)))

                            groupRow(col) = qagg / qtot
                        Else
                            Dim q2 = From r In groupRow.GetChildRows("GroupToLink")
                                     Select CStr(r.GetParentRow("SourceToLink")(col.ColumnName)) Distinct

                            groupRow(col) = String.Join(vbCrLf, q2.ToArray())
                        End If
                    End If
                End If
            Next
        End Sub

        Private Sub CheckBox_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            'TODO: Add event handler implementation here.
            sender.DataContext.Visibility = Visibility.Visible
        End Sub

        Private Sub CheckBox_Unchecked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            'TODO: Add event handler implementation here.
            sender.DataContext.Visibility = Visibility.Collapsed
        End Sub

        Private HideShowAllState As Boolean = True
        Private Sub HideShowAll_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles HideShowAll.Click
            HideShowAllState = Not HideShowAllState
            HideShowAll.Content = If(HideShowAllState, _
                    "Cacher toutes les colonnes", _
                    "Afficher toutes les colonnes")
            For Each elt In simpleDataGrid.Columns
                elt.Visibility = If(HideShowAllState, Windows.Visibility.Visible, Windows.Visibility.Collapsed)
            Next
        End Sub


        Private Sub AdvancedOptions_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles AdvancedOptions.Click
            Dim q = From elt As DataGridBoundColumn In simpleDataGrid.Columns Select New ViewModelCalculDetailleGridColumnOption(elt)

            Dim opts As New ViewModelCalculDetailleOption(simpleDataGrid)
            opts.Columns = New ObjectModel.ObservableCollection(Of ViewModelCalculDetailleGridColumnOption)(q.AsEnumerable())

            Dim ui = ModuleTauxGaranti._app.GetService(Of IUIService)()

            ui.ShowMessage(opts)
        End Sub

        Private Sub simpleDataGrid_AutoGeneratedColumns(ByVal sender As Object, ByVal e As System.EventArgs) Handles simpleDataGrid.AutoGeneratedColumns
            Dim opt = New ViewModelCalculDetailleOption(simpleDataGrid)

            Dim sinfo = Application.GetResourceStream( _
                New Uri("/OMANE.Modules.TauxGarantis;component/calcul_detaillee_modele_2.view", System.UriKind.Relative))

            opt.LoadParametresExecute(sinfo.Stream)
        End Sub
    End Class
End Namespace